package com.redare.dev.smart.generator.sdk.utils;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author hxw
 */
public class CommonUtils {

    private static final Pattern NUM_PATTERN = Pattern.compile("^-?\\d+(\\.\\d+)?$");

    private static final Pattern TRIM_PATTERN = Pattern.compile("\\s*|\t|\r|\n");

    /**
     * 校验固定值是否合法
     */
    public static boolean isLegalNum(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        String suffix = "%";
        if (StringUtils.endsWith(str, suffix)) {
            str = str.replaceAll(suffix, "");
        }
        return NUM_PATTERN.matcher(str).matches();
    }

    /**
     * 将百分比字符串转为小数值 （ 如：20% to 0.2）
     * @param stringNum
     * @return
     */
    public static String convertData(String stringNum) {
        String regex = "%";
        if (stringNum.endsWith(regex)) {
            stringNum = stringNum.replaceAll(regex, "");
            BigDecimal decimal = new BigDecimal(stringNum);
            return decimal.divide(new BigDecimal("100"), 5, BigDecimal.ROUND_HALF_UP).toString();
        }
        return stringNum;
    }

    /**
     * 获取添加的表达式存在未知编码code数量
     * @param formValue 表达式
     * @param codeList 已添加税金项code集合。(格式：[Taxes01,Taxes02])
     * @return 未知编码code数量
     */
    public static long getCount(String formValue, List<String> codeList) {
        List<String> list = CommonUtils.getValueList(formValue);
        return list.stream().filter(item -> !CommonUtils.isLegalNum(item) && !codeList.contains(item)).count();
    }

    public static List<String> getValueList(String formValue) {
        List<String> symbolList = Arrays.asList("+", "-", "*", "/", "(", ")");
        String[] strings = formValue.split("");
        StringBuilder sb = new StringBuilder();
        //空格
        String symbol = " ";
        for (String s : strings) {
            int index = symbolList.indexOf(s);
            if (index != -1) {
                sb.append(symbol);
            }else {
                sb.append(s);
            }
        }
        String[] strings1 = sb.toString().split(symbol);
        List<String> list = new ArrayList<>(Arrays.asList(strings1));
        return list.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
    }

    /**
     * 将表达式的引用code替换为1,
     */
    public static String replaceExpressionToOne(String expression) {
        List<String> valueList = CommonUtils.getValueList(expression);
        for (String value : valueList) {
            if (!CommonUtils.isLegalNum(value) && StringUtils.contains(expression, value)) {
                expression = expression.replaceAll(value, "1");
            }
        }
        return expression;
    }

    /**
     * 将计算后的字符串金额保留两位小数，并根据千分位以逗号分割
     * @return
     */
    public static String subTwoPoint(String moneyStr) {
        if (StringUtils.isBlank(moneyStr)) {
            return "0.00";
        }
        BigDecimal decimal = new BigDecimal(moneyStr).setScale(2, BigDecimal.ROUND_HALF_UP);
        return CommonUtils.moneySet(decimal);
    }

    /**
     * @description 金额转换千位符金额
     * @param money
     * @return
     */
    public static String moneySet(BigDecimal money){
        String moneyStr = null;
        if( money == null || money.compareTo(new BigDecimal("0.00") ) == 0){
            moneyStr = "0.00";
        }else{
            DecimalFormat df = new DecimalFormat("#,###.00");
            moneyStr = String.format("%,.2f",money);
        }
        return moneyStr;
    }

    /**
     * 去除字符串中的换行符、制表符、空格
     * @param str
     * @return
     */
    public static String stringTrim(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        return TRIM_PATTERN.matcher(str).replaceAll("");
    }

}
